<?php

namespace app\fx\controller\product;

use app\fx\model\DataProductShop;
use app\fx\model\DataProductCate;
use app\fx\model\DataBase;
use app\fx\service\ShopService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
use think\admin\helper\QueryHelper;
use think\facade\Db;
use think\facade\Request;

/**
 * 商品管理
 * Class Shop
 * @package app\fx\controller\product
 */
class Shop extends Controller
{
    /**
     * 商品管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function index()
    {
        $cates = DataProductCate::mk()->where('pid', 0)->where('status', 0)->column('*');
        foreach ($cates as $k => $v) {
            $cat1_arr = DataProductCate::mk()->where('pid', $v['id'])->where('status', 0)->column('*');
            foreach ($cat1_arr as $sk => $sv) {
                $cat2_arr = DataProductCate::mk()->where('pid', $sv['id'])->where('status', 0)->column('*');
                if (count($cat2_arr) > 0) {
                    $cat1_arr[$sk]['sub'] = $cat2_arr;
                }
            }
            $cates[$k]['sub'] = $cat1_arr;
        }
        $this->cates = $cates;

        DataProductShop::mQuery($this->get)->layTable(function () {
            $this->title = '列表';

            $this->tit_arr = ['ID', '中文名', '英文名', '商品中文规格', '商品英文规格', '计量单位组', '主计量单位', '采购计量单位', '销售计量单位', '库存计量单位', '报关计量单位', '报关助记符', '报关商品编码', '报关商品中文名', '报关商品英文名', '单箱净重', '单箱毛重', '单箱体积', '单箱数量', '箱子长', '箱子宽', '箱子高', '出口属性', '进口属性', '配件属性', '内销属性', '包材属性', '内购属性', '自制属性', '委外属性', '保税属性', 'OEM号', '颜色', '商标', '销售单价', '备注', '首选供应商', '默认采购币种', '默认采购单价', '拥有人', '生产说明', '增值税率%'];

            $this->id_arr = ['id', 'name', 'ename', 'guige', 'eguige', 'int', 'mint', 'bint', 'sint', 'tint', 'bgint', 'bgicon', 'bgcode', 'bgname', 'bgename', 'pweight', 'pmweight', 'ptj', 'pnum', 'plong', 'pkuan', 'pheight', 'cktype', 'jktype', 'pjtype', 'nxtype', 'mtype', 'ngtype', 'zztype', 'wwtype', 'bstype', 'oem', 'color', 'sb', 'price', 'beizhu', 'fgys', 'mbz', 'mprice', 'owner', 'des', 'ratio'];

        }, function (QueryHelper $query) {
            $cat1 = request()->get('cat1');
            $cat2 = request()->get('cat2');
            $cat3 = request()->get('cat3');

            // $query->like(['cat1', 'cat2', 'cat3', 'no', 'name']);
            if (!empty($cat3)) {
                $query->equal('cat3')->like(['no', 'name']);
            } elseif (!empty($cat2)) {
                $query->equal('cat2')->like(['no', 'name']);
            } elseif (!empty($cat1)) {
                $query->equal('cat1')->like(['no', 'name']);
            } else {
                $query->like(['no', 'name']);
            }
        });
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        $cat_pos = DataProductCate::mk()->where('status', 0)->column('name', 'id');//print_r($cat_pos);
        foreach ($data as $k => $v) {
            $cat = "";
            //echo $cat_pos[$v['cat1']];
            //echo $v['cat1'];echo $cat_pos[$v['cat1']];
            if (!empty($v['cat1']) && !empty($cat_pos[$v['cat1']])) {
                $cat .= $cat_pos[$v['cat1']];
            }//echo $cat;
            if (!empty($v['cat2']) && !empty($cat_pos[$v['cat2']])) {
                $cat .= ' > ' . $cat_pos[$v['cat2']];
            }
            if (!empty($v['cat3']) && !empty($cat_pos[$v['cat3']])) {
                $cat .= ' > ' . $cat_pos[$v['cat3']];
            }
            //echo $cat;
            $data[$k]['cat'] = $cat;

            if ($v['status'] == 0) {
                $data[$k]['status_name'] = "上架";
            }
            if ($v['status'] == 1) {
                $data[$k]['status_name'] = "下架";
            }
        }
    }

    /**
     * 添加商品
     * @auth true
     */
    public function add()
    {
        DataProductShop::mForm('form', 'id');
    }

    /**
     * 编辑商品
     * @auth true
     */
    public function edit()
    {
        DataProductShop::mForm('form');
    }



    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {

        if ($this->request->isGet()) {

            $this->cates = DataProductCate::treeTable(true);
            //print_r($this->cates);
            $data['cat'] = "";
            if (!empty($data['cat1'])) {
                $data['cat'] .= $data['cat1'];
            }
            if (!empty($data['cat2'])) {
                $data['cat'] .= "," . $data['cat2'];
            }
            if (!empty($data['cat3'])) {
                $data['cat'] .= "," . $data['cat3'];
            }


        } else {

            //print_r($data);
            $cat_arr = explode(",", $data['cat']);
            if (!empty($cat_arr[0])) {
                $data['cat1'] = $cat_arr[0];
            } else {
                $data['cat1'] = 0;
            }
            if (!empty($cat_arr[1])) {
                $data['cat2'] = $cat_arr[1];
            } else {
                $data['cat2'] = 0;
            }
            if (!empty($cat_arr[2])) {
                $data['cat3'] = $cat_arr[2];
            } else {
                $data['cat3'] = 0;
            }
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    public function state()
    {
        DataProductShop::mSave($this->_vali([
            'status.in:0,1' => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 基础管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function base()
    {
        $this->pass = DataBase::mk()->where('id', 1)->value('data');
        $this->ip_str = DataBase::mk()->where('id', 2)->value('data');
        $this->fetch();
    }

    public function alert_pass($pass)
    {
        $up = DataBase::mk()->where('id', 1)->update(['data' => $pass]);
        if ($up !== false) {
            return 'y';
        } else {
            return 'n';
        }
    }

    public function alert_ip($ip)
    {
        $up = DataBase::mk()->where('id', 2)->update(['data' => $ip]);
        if ($up !== false) {
            return 'y';
        } else {
            return 'n';
        }
    }

    /**
     * 批量删除
     * @auth true
     * @return void
     */
    public function remove()
    {
        DataProductShop::mDelete();
    }

    public function check_no($no, $id)
    {
        //echo $no;
        //echo $id;
        $check = DataProductShop::mk()->where('no', $no)->where('id', '<>', $id)->find();
        if ($check) {
            return 'y';
        } else {
            return 'n';
        }
    }
}